package com.power.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.repository.query.Param;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.github.pagehelper.PageInfo;
import com.power.model.jdao.IRoleDao;
import com.power.model.pojo.Role;
import com.power.service.RoleService;

@RestController
@RequestMapping("/role")
public class RoleController {
	@Autowired
	RoleService rs;
	List<Role> list;
	@Autowired
	IRoleDao dao;
	/**
	  * 角色管理 查询所有角色
	 * @return
	 */
	@RequestMapping("/all")
	public PageInfo<Role> selectAll(@RequestParam("curpage")Integer pageNo,@RequestParam("pagesize")Integer pageSize){
		System.out.println(pageNo+":::"+pageSize);
		list=rs.selectAll(pageNo, pageSize);
		PageInfo<Role> pager= new PageInfo<Role>(list);
		return pager;
	}
	@RequestMapping("/where")
	public PageInfo<Role> selectBywhere(String rname,int isstatus,@RequestParam("curpage")Integer pageNo,@RequestParam("pageSize")Integer pageSize){
		System.out.println(pageNo+""+pageSize+""+rname+""+isstatus);
		list=rs.selectBywhere(pageNo, pageSize, rname, isstatus); 
		
		PageInfo<Role> pager= new PageInfo<Role>(list);
		return pager;
	}
	@RequestMapping("/add")
	public boolean addRole(String rname,Integer isstatus,String createby) {
	boolean isOk=rs.selectByRname(rname);
	if(isOk) {
		rs.addRole(rname, isstatus, createby);
		return true;
	}
	return false;
	}
	@RequestMapping("/del")
	public boolean delRole(Integer rid) {
		if(rid==1) {
			return false;
		}else {
		rs.delRole(rid);
		return true;
		}
	
	}
	
	@RequestMapping("/update")
	public void updateRole(@Param("rid")Integer rid,@Param("isstatus")Integer isstatus) {
		System.out.println(rid+""+isstatus);
		rs.updataRole(rid, isstatus);
	}	
	@RequestMapping("/rname")
	public List<Role> selectRoleRname(){
		list=rs.selectRoleRname();
		return list;
	}
	/**
	 * 角色配置权限 查询所有角色
	 * @return
	 */
	@RequestMapping("/alls")
	public List<Role> selectAlls(){
		list=rs.selectAlls();
		return list;
	}
	@RequestMapping("/getByRids")
	public List<Role> selectByRids(Integer uid){
		list=rs.selectByUid(uid);
		return list;
	}
	@RequestMapping("/getMenuByrid")
	public List<Role> selectMenuByRid(Integer rid){
		System.out.println(rid);
		return rs.selectMenuByRid(rid);
	}
	
	
	@RequestMapping("/updateMenuAndRole")
	public void updateMenuAndRole(Integer rid,String str){
		String [] strarr=str.split(",");
    	Integer [] mids=new Integer[strarr.length];
    	for(int i=0;i<strarr.length;i++) {
    		mids[i]=Integer.parseInt(strarr[i]);
    		System.out.println(mids[i]);
    	}
		System.out.println(rid+"--"+mids);
		rs.update(rid, mids);
	}

	
}
 